package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.AppType;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgPlatformMapper;
import com.ygqh.baby.po.YgPlatform;
import com.ygqh.baby.po.YgPlatformExample;
import com.ygqh.baby.service.YgPlatformService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author admin
 */
@Service
public class YgPlatformServiceImpl implements YgPlatformService {

	@Autowired
	private YgPlatformMapper ygPlatformMapper;

	@Override
	public List<YgPlatform> find() {
		YgPlatformExample example = new YgPlatformExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygPlatformMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgPlatformExample example = new YgPlatformExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygPlatformMapper.countByExample(example);
	}

	@Override
	public YgPlatform findById(Long id) {
		return ygPlatformMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgPlatform ygPlatform) {
		return ygPlatformMapper.insertSelective(ygPlatform);
	}

	@CacheEvict(cacheNames = "redis", key = "'platform_'+#ygPlatform.platformNo")
	@Override
	public int update(YgPlatform ygPlatform) {
		return ygPlatformMapper.updateByPrimaryKeySelective(ygPlatform);
	}

	@Override
	public ResultSet<YgPlatform> search(QueryInfo queryInfo, String q) {
		int count = ygPlatformMapper.countSuper(q);
		if (count == 0) {
			return new ResultSet<>();
		}
		List<YgPlatform> list = ygPlatformMapper.selectSuper(queryInfo, q);
		return new ResultSet<>(count, list);
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgPlatformExample example = new YgPlatformExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgPlatform record = new YgPlatform();
		record.setStatus(DataStatus.Delete);

		return ygPlatformMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgPlatform ygPlatform) {
		int r;
		if (ygPlatform.getId() != null) {
			r = this.update(ygPlatform);
		} else {
			r = this.save(ygPlatform);
		}
		return r;
	}

	@Override
	public YgPlatform findByAppId(String appId, AppType appType) {
		YgPlatformExample example = new YgPlatformExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name())
				.andAppIdEqualTo(appId).andAppTypeEqualTo(appType.name());
		List<YgPlatform> list = ygPlatformMapper.selectByExample(example);
		if (CollectionUtils.isNotEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Cacheable(cacheNames = "redis", key = "'platform:platformNo_'+#platformNo")
	@Override
	public YgPlatform findByPlatformNo(String platformNo) {
		return findByPlatformNo(platformNo, AppType.Small);
	}

//	@Cacheable(cacheNames = "redis", key = "'platform:platformNo_'+#platformNo+'_appType_'+#appType")
	@Override
	public YgPlatform findByPlatformNo(String platformNo, AppType appType) {
		if (StringUtils.isEmpty(platformNo) || appType == null) {
			return null;
		}
		YgPlatformExample example = new YgPlatformExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name())
				.andPlatformNoEqualTo(platformNo)
				.andAppTypeEqualTo(appType.name());
		List<YgPlatform> list = ygPlatformMapper.selectByExample(example);
		return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
	}
}
